package au.com.handsunited.web.validator.project;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import au.com.handsunited.model.Project;
import au.com.handsunited.model.validators.entity.ProjectValidator;
import au.com.handsunited.web.command.project.ProjectCommand;

/**
 * @author Arun Chandrasekhar
 *
 */
public class ProjectCommandValidator implements Validator {

	private static Logger logger = LoggerFactory.getLogger(ProjectCommandValidator.class);


	private ProjectValidator projectValidator;


	/**
	 * @param projectValidator
	 */
	public ProjectCommandValidator(ProjectValidator projectValidator) {
		super();
		this.projectValidator = projectValidator;
	}

	/**
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@Override
	public boolean supports(Class clazz) {
		return clazz.isAssignableFrom(ProjectCommand.class);
	}

	/**
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	public void validate(Object target, Errors errors) {
		ProjectCommand projectCommand = (ProjectCommand) target;
		Project project = projectCommand.getProject();

		try {
			errors.pushNestedPath("project");
			ValidationUtils.invokeValidator(projectValidator, project, errors);
		} finally {
			errors.popNestedPath();
		}

	}

}
